package com.chengyu.core.controller.common;




import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chengyu.core.constants.WeChatConstants;
import com.chengyu.core.exception.CommonException;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.request.*;
import com.chengyu.core.response.FrontLoginConfigResponse;
import com.chengyu.core.response.LoginResponse;
import com.chengyu.core.result.CommonResult;
import com.chengyu.core.service.LoginService;
import com.chengyu.core.service.SystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@RestController
@RequestMapping("common/loginWx")
@Api(tags = "用户 -- 登录注册")
public class LoginWxController {

    @Autowired
    private LoginService loginService;


    @Autowired
    private SystemConfigService systemConfigService;

    @ApiOperation(value = "获取登录配置")
    @RequestMapping(value = "/config", method = RequestMethod.GET)
    public CommonResult<FrontLoginConfigResponse> getLoginConfig() {
        return CommonResult.success(loginService.getLoginConfig());
    }

//    @ApiOperation(value = "手机号验证码登录")
//    @RequestMapping(value = "/mobile/captcha", method = RequestMethod.POST)
//    public CommonResult<LoginResponse> phoneCaptchaLogin(@RequestBody @Validated LoginMobileRequest loginRequest) {
//        return CommonResult.success(loginService.phoneCaptchaLogin(loginRequest));
//    }
//
//    @ApiOperation(value = "手机号密码登录")
//    @RequestMapping(value = "/mobile/password", method = RequestMethod.POST)
//    public CommonResult<LoginResponse> phonePasswordLogin(@RequestBody @Validated LoginPasswordRequest loginRequest) {
//        return CommonResult.success(loginService.phonePasswordLogin(loginRequest));
//    }

//    @ApiOperation(value = "退出")
//    @RequestMapping(value = "/logout", method = RequestMethod.GET)
//    public CommonResult<String> loginOut(HttpServletRequest request) {
//        loginService.loginOut(request);
//        return CommonResult.success();
//    }

//    @ApiOperation(value = "发送短信登录验证码")
//    @RequestMapping(value = "/send/code", method = RequestMethod.POST)
//    public CommonResult<String> sendCode(@RequestBody @Validated SendCodeRequest request) {
//        if (loginService.sendLoginCode(request.getPhone())) {
//            return CommonResult.success("发送成功");
//        }
//        return CommonResult.failed("发送失败");
//    }

    @ApiOperation(value = "微信公众号号授权登录")
    @RequestMapping(value = "/wechat/public", method = RequestMethod.POST)
    public CommonResult<LoginResponse> wechatPublicLogin(@RequestBody @Validated WechatPublicLoginRequest request) throws ServiceException {
//        System.out.println("/wechat/public/wechat/public/wechat/public/wechat/public/wechat/public");
//        System.out.println(request);
        return CommonResult.success(loginService.wechatPublicLogin(request));

//        String appId = systemConfigService.getValueByKey(WeChatConstants.WECHAT_PUBLIC_APPID);
//        if (StrUtil.isBlank(appId)) {
//            throw new CommonException("微信公众号appId未设置");
//        }
//        String secret = systemConfigService.getValueByKey(WeChatConstants.WECHAT_PUBLIC_APPSECRET);
//        if (StrUtil.isBlank(secret)) {
//            throw new CommonException("微信公众号secret未设置");
//        }
//
//
//        String code  = request.getCode();
////        String state = request.getParameter("state");
//        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
//                + "appid=" + appId
//                + "&secret=" + secret
//                + "&code=" + code
//                + "&grant_type=authorization_code";
//        JSONObject jsonObject = JSON.parseObject(HttpUtil.get(url));
//
//        System.out.println(jsonObject);
//        Integer errcode = jsonObject.getInteger("errcode");
//        System.out.println(errcode);
//        if (errcode != null) {
//            String errmsg = jsonObject.getString("errmsg");
//            throw new CommonException("【微信回调错误】错误码：" + errcode + "，错误信息：" + errmsg);
//        }
    }

    @ApiOperation(value = "微信登录小程序授权登录")
    @RequestMapping(value = "/wechat/routine", method = RequestMethod.POST)
    public CommonResult<LoginResponse> wechatRoutineLogin(@RequestBody @Validated RegisterThirdUserRequest request) throws ServiceException {
        return CommonResult.success(loginService.wechatRoutineLogin(request));
    }

    @ApiOperation(value = "微信注册绑定手机号")
    @RequestMapping(value = "/wechat/register/binding/phone", method = RequestMethod.POST)
    public CommonResult<LoginResponse> wechatRegisterBindingPhone(@RequestBody @Validated WxBindingPhoneRequest request) {
        return CommonResult.success(loginService.wechatRegisterBindingPhone(request));
    }

    @ApiOperation(value = "微信登录App授权登录")
    @RequestMapping(value = "/wechat/app/login", method = RequestMethod.POST)
    public CommonResult<LoginResponse> wechatAppLogin(@RequestBody @Validated RegisterAppWxRequest request) throws ServiceException {
        return CommonResult.success(loginService.wechatAppLogin(request));
    }

    @ApiOperation(value = "ios登录")
    @RequestMapping(value = "/ios/login", method = RequestMethod.POST)
    public CommonResult<LoginResponse> ioslogin(@RequestBody @Validated IosLoginRequest loginRequest)  {
        return CommonResult.success(loginService.ioslogin(loginRequest));
    }

    @ApiOperation(value = "校验token是否有效")
    @RequestMapping(value = "/token/is/exist", method = RequestMethod.POST)
    public CommonResult<Boolean> tokenIsExist() {
        return CommonResult.success(loginService.tokenIsExist());
    }

}



